<template>
    <div class="search">
      <div class="outer">
        <input
          v-model="searchStore.searchInput"
          placeholder="输入你备课的主题/内容/目标"
          class="search-input"
        />
        <el-icon color="#ECACFC" style="margin-right: 10px;" @click="sendRequire"><Promotion/></el-icon>
      </div>
    </div>
</template>

<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router';
import { Promotion } from '@element-plus/icons-vue'
import { useSearchStore } from '@/stores/search';
const searchStore = useSearchStore()
const router = useRouter()
const route = useRoute()
function sendRequire(){
  let type = 'TeachingPlan'
  if (route.path === '/aiSource') {
    type = 'QuestionGenerator';
  } else if (route.path === '/aiAnalysis') {
    type = 'SubjectAnalysis';
  }
  router.push({
    name: 'chat',
    query: {
      type: type,
      message: searchStore.searchInput
    }
  })
  if(route.path === '/aiDesign'){
    let designHistory = JSON.parse(localStorage.getItem('designHistory') || '[]');
    designHistory.unshift(searchStore.searchInput);
    if (designHistory.length > 5) {
        designHistory.pop();
    }
    localStorage.setItem('designHistory', JSON.stringify(designHistory));
  } else if (route.path === '/aiSource') {
    let sourceHistory = JSON.parse(localStorage.getItem('sourceHistory') || '[]');
    sourceHistory.unshift(searchStore.searchInput);
    if (sourceHistory.length > 5) { 
        sourceHistory.pop();
    }
    localStorage.setItem('sourceHistory', JSON.stringify(sourceHistory));
  } else if (route.path === '/aiAnalysis') {
    let analysisHistory = JSON.parse(localStorage.getItem('analysisHistory') || '[]');
    analysisHistory.unshift(searchStore.searchInput);
    if (analysisHistory.length > 5) { 
        analysisHistory.pop();
    }
    localStorage.setItem('analysisHistory', JSON.stringify(analysisHistory));
  }
}
</script>

<style scoped lang="less">
@import url('@/components/Search/index.less');
</style>